package com.util;

import java.util.List;

public class Page {
	private List list; //要返回的某一页得记录
	private int allRow; //总记录数
	private int totalPage; //总页数
	private int currentPage; //当前页
	private int pageSize;    //每页记录数
	private boolean isFirstPage; //是否为第一页
	private boolean isLastPage; //是否为最后一页
	private boolean hasPreviousPage; //是否有前一页
	private boolean hasNextPage; //是否有下一页
	private int startPage = -1;//显示的起始页
	private int endPage = -1;//显示的结束页
	private int pageNumShow = 5;//可见页数

	public Page(){
		
	}
	
	public Page(int allRow, int pageSize, int currentPage){
		this.allRow=allRow;
		this.pageSize = pageSize;
		this.currentPage = currentPage;
		this.totalPage = Page.countTotalPage(pageSize, allRow);
		this.init();
	}
	
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public int getAllRow() {
		return allRow;
	}
	public void setAllRow(int allRow) {
		this.allRow = allRow;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}	
	public int getStartPage() {
		return startPage;
	}
	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	public int getPageNumShow() {
		return pageNumShow;
	}
	public void setPageNumShow(int pageNumShow) {
		this.pageNumShow = pageNumShow;
	}
	
	/**
	 * 
	 * 初始化分页信息
	 * 
	 * */
	public void init()
	{
		this.isFirstPage = isFirstPage();
		this.isLastPage = isLastPage();
		this.hasPreviousPage = isHasPreviousPage();
		this.hasNextPage = isHasNextPage();
		initPageShow();
	}
	
	/**
	 * 
	 * 初始化分页的页面显示
	 * 
	 * */
	public void initPageShow(){
		if(this.getTotalPage()<this.pageNumShow){
			this.startPage = 1;
			this.endPage = this.getTotalPage();
		}else{
			if(this.currentPage<=this.pageNumShow/2){
				this.startPage = 1;
				this.endPage = this.pageNumShow;
			}else if(this.currentPage>this.pageNumShow/2 
						&& this.currentPage<this.totalPage-this.pageNumShow/2){
				this.startPage = this.currentPage-this.pageNumShow/2;
				this.endPage = this.currentPage+this.pageNumShow/2;
			}else if(this.currentPage>=this.totalPage-this.pageNumShow/2){
				this.startPage = this.totalPage-this.pageNumShow;
				this.endPage = this.totalPage;
			}else{
				this.startPage=1;
				this.endPage=1;
			}
		}
	}
	
	/**
	 * 一下判断页得信息，只需getter方法（is方法）即可
	 * @return
	 * */
	public boolean isFirstPage() {
		return currentPage == 1;
	}

	public boolean isLastPage() {
		return currentPage == totalPage;
	}

	public boolean isHasPreviousPage() {
		return currentPage != 1;
	}

	public boolean isHasNextPage() {
		return currentPage !=totalPage;
	}
	public boolean getHasPreviousPage(){
		return this.hasPreviousPage;
	}
	public boolean getHasNextPage(){
		return this.hasNextPage;
	}
	/**
	 * 计算总页数，静态方法，供外部直接通过类名调用
	 * @param pageSize 每页记录数
	 * @param allRow 总记录数
	 * @return 总页数
	 * */
	public static int countTotalPage(final int pageSize,final int allRow)
	{
		int totalPage = (allRow % pageSize == 0?allRow/pageSize:allRow/pageSize+1);
		return totalPage;
	}
	
	/**
	 * 计算当前页开始记录
	 * @param pageSize 每页记录数
	 * @param currentPage 当前第几页
	 * @return 当前页开始记录号
	 * */
	public static int countOffset(final int pageSize,final int currentPage)
	{
		final int offset = pageSize*(currentPage-1);
		return offset;
	}
	
	/**
	 * 计算当前页，若为0或者请求的URL中没有"?page=",则用1代替
	 * @param page 传入的参数（可能为空，即0，则返回1）
	 * @return 当前页
	 * */
	public static int countCurrentPage(int page){
		final int curPage = (page==0?1:page);
		return curPage;
	}


	
}
